# -*- coding: utf-8 -*-
# @Time : 2020/12/4 22:04
# @Author : Steven
# @QQ : 2621228281
# @Email : 2621228281@qq.com
# @File : douyinJiexi.py
# @Software: PyCharm
import re
import requests
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


def run():

    print('\n')
    print('=========================================================================')
    print('           【抖音直播源解析小工具】By：Steven QQ:2621228281')
    print('=========================================================================')
    print('请注意只复制网址，不复制文字进入,如：https://v.douyin.com/JXkLCLX/')
    print('\n')
    print('=========================================================================')
    print('输入后按回车键获取直播源')
    print('=========================================================================')
    url = str(input('请输入抖音短网址：'))
    print('=========================================================================')

    headers = {
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,ja;q=0.7',
        'sec-ch-ua': '"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"',
        'sec-ch-ua-mobile': '?0',
        'sec-fetch-dest': 'document',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-site': 'none',
        'sec-fetch-user': '?1',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 Safari/537.36',
    }

    response = requests.get(url,headers=headers,verify=False)
    # print(response.text)
    live_stream = re.findall(r'hls_pull_url":"(.*?)",',response.text)[0]
    with open('解析直播源.txt','a',encoding='utf-8')as f:
        f.write(live_stream+'\n')
        f.close()
    print(live_stream)
    print('=========================================================================')

if __name__ == '__main__': 
    while True:
        run()
        choice = input('是否继续解析其他直播间地址(yes/no)：')
        print('=========================================================================')
        if choice == 'no': 
            quit